package com.vichat.common.util;

import com.vichat.common.sms.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;


public class WarnUtil {

	public static ConcurrentHashMap<String,Long> warnMap = new ConcurrentHashMap<String,Long>();
	public static ConcurrentHashMap<String,Long> warnnumMap = new ConcurrentHashMap<String,Long>();
	public static ConcurrentHashMap<String,Long> warnstartMap = new ConcurrentHashMap<String,Long>();
	private static Logger logger = LoggerFactory.getLogger(WarnUtil.class);

	private static String defaultWarnMobile = "18802106866";
	
	public static String[] alertMobiles = {"18802106866","18621669113","18905510630"};
	
	/*
	 * warnType billlog billntf roomevent imevent chatevent
	 */
	public  static void postWarn(String warnType,String warnInfo){
		try{
			boolean sendwarn = false;
			long curtime = System.currentTimeMillis();
			if(warnMap.containsKey(warnType)){
				Long warntime = warnMap.get(warnType);
				if(warntime.longValue()<curtime-900000){
					sendwarn = true;
				}
			}else{
				sendwarn = true;
			}
			long stattime = curtime;
			if(warnstartMap.contains(warnType)){
				stattime = warnstartMap.get(warnType);
			}else{
				warnstartMap.put(warnType, new Long(curtime));
			}
			long warnnum = 0;
			if(warnnumMap.contains(warnType)){
				warnnum = warnnumMap.get(warnType).longValue();
				
			}
			warnnum++;
			//如果超过10分钟还没触发，则不告警
			if(curtime-stattime>600000){
				warnnum = 1;
				warnstartMap.put(warnType, new Long(curtime));
			}

			warnnumMap.put(warnType, warnnum);


			//10分钟内超过3次才告警
			if(sendwarn && warnnum > 3){

				warnMap.put(warnType, new Long(curtime));
				SmsUtil.sendWarningInfo(warnInfo, defaultWarnMobile);
				warnnumMap.put(warnType, 0l);
				warnstartMap.remove(warnType);
			}
		}catch(Exception e){
			logger.error("发送异常告警失败"+warnType,e);
		}
	}

	public  static void postAlert(String warnType,String warnInfo){
		try{
			boolean sendwarn = false;
			long curtime = System.currentTimeMillis();
			if(warnMap.containsKey(warnType)){
				Long warntime = warnMap.get(warnType);
				//距离上次告警15分钟
				if(warntime.longValue()<curtime-900000){
					sendwarn = true;
				}
			}else{
				//未发送告警
				sendwarn = true;
			}
			long stattime = curtime;
			if(warnstartMap.containsKey(warnType)){
				stattime = warnstartMap.get(warnType);
			}else{
				warnstartMap.put(warnType, new Long(curtime));
			}
			long warnnum = 0;
			if(warnnumMap.containsKey(warnType)){
				warnnum = warnnumMap.get(warnType).longValue();

			}
			warnnum++;
			//如果超过10分钟还没触发，则不告警
			if(curtime-stattime>600000){
				warnnum = 1;
				warnstartMap.put(warnType, new Long(curtime));
			}

			warnnumMap.put(warnType, warnnum);


			//10分钟内超过3次才告警
			if(sendwarn && warnnum > 3){

				warnMap.put(warnType, new Long(curtime));
				SmsUtil.sendWarningInfo(warnInfo, alertMobiles);
				warnnumMap.put(warnType, 0l);
				warnstartMap.remove(warnType);
			}
		}catch(Exception e){
			logger.error("发送异常告警失败"+warnType,e);
		}
	}
	
}
